package com.snail.auth.service;

import com.common.util.StringUtil;
import com.snail.auth.dao.AuthOrgRep;
import com.snail.auth.dao.AuthUserRep;
import com.snail.auth.entity.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class AuthUserService {
    @Autowired
    private AuthUserRep userRep;
    @Autowired
    private AuthOrgRep authOrgRep;

    public void save(AuthUser entity) {
        if (entity.getFlag() == null) {
            entity.setFlag(0);
        }
        userRep.saveAndFlush(entity);
    }

    public void del(Long id) {
        AuthUser user = getByKey(id);
        user.setFlag(1);
        userRep.saveAndFlush(user);
    }

    public List<AuthUser> listByHql(Map<String, Object> params) {
        StringBuilder hql = new StringBuilder("from AuthUser where flag=0");
        if (!StringUtil.isEmpty(params.get("orgId"))) {
            hql.append(" and org.id = " + params.get("orgId"));
        }
        return userRep.execHql(hql.toString());
    }

    public AuthUser getByKey(Long id) {
        Optional optional = userRep.findById(id);
        if (optional.isPresent()) {
            return (AuthUser) optional.get();
        }
        return null;
    }

    public List<AuthUser> findByName(String name){
        List<AuthUser> users = userRep.execHql(" from AuthUser where name ='"+name+"' and flag=0");
        return users;
    }
    /**
     * 通过username查找用户信息;
     */
    public AuthUser findByLoginName(String loginName) {
        return userRep.findByLoginName(loginName);
    }

    public AuthUser findByLoginNameAndFlag(String name, Integer flag) {
        return userRep.findByLoginNameAndFlag(name, flag);
    }

    /**
     * 获取符合element使用的多级结构树
     */
    public List<Map<String, Object>> treeData(Map<String, Object> params) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<Map<String, Object>> datas = authOrgRep.execSql("select CONCAT('org_',id) as id, name, CONCAT('org_',parent_id) as parent_id from auth_org where 1=1 order by sort");
        datas.addAll(userRep.execSql("select id, name, CONCAT('org_',org_id) as parent_id from auth_user where flag=0"));
        Map<String, List<Map<String, Object>>> midMap = new LinkedHashMap<>();
        for (Map<String, Object> data : datas) {
            if (!midMap.containsKey(data.get("parent_id").toString())) {
                midMap.put(data.get("parent_id").toString(), new ArrayList<>());
            }
            midMap.get(data.get("parent_id").toString()).add(data);
        }
        return pushTreeData(result, midMap, "org_0");
    }

    public List<Map<String, Object>> pushTreeData(List<Map<String, Object>> result, Map<String, List<Map<String, Object>>> midMap, String parentCode) {
        for (Map<String, Object> firstMap : midMap.get(parentCode)) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", firstMap.get("id"));
            map.put("label", firstMap.get("name"));
            if (midMap.containsKey(firstMap.get("id").toString())) {
                List<Map<String, Object>> childs = new ArrayList<>();
                for (Map<String, Object> secondMap : midMap.get(firstMap.get("id").toString())) {
                    Map<String, Object> child = new HashMap<>();
                    child.put("value", secondMap.get("id"));
                    child.put("label", secondMap.get("name"));
                    if (midMap.containsKey(secondMap.get("id").toString())) {
                        child.put("children", pushTreeData(new ArrayList<Map<String, Object>>(), midMap, secondMap.get("id").toString()));
                    }
                    childs.add(child);
                }
                map.put("children", childs);
            }
            result.add(map);
        }
        return result;
    }

}